package com.java.simple.study.jvm.memstruct.methodarea;

/**
 * @author zhoujunlin
 * @date 2021/12/12
 * @desc 小测试
 **/
public class Demo3_2 {

    public static void main(String[] args) {
        String s1 = "a";
        String s2 = "b";
        String s3 = "a" + "b";
        String s4 = s1 + s2;
        String s5 = "ab";
        String s6 = s4.intern();
        // StringTable["a", "b", "ab"]
        // heap s1  s2  new String("ab")
        // s6 串池  s4 intern -> 串池
        // 问
        System.out.println(s3 == s4); // false
        System.out.println(s3 == s5); // true
        System.out.println(s3 == s6); // true

        String x2 = new String("c") + new String("d");
        String x1 = "cd";
        x2.intern();  // 串池有 不放入 x2仍然堆中对象
        // StringTable["a", "b", "ab", "c", "d", "cd"]
        // heap s1  s2  new String("ab") new String("c") new String("d") new String("cd")
        // 灵魂拷问，如果调换了【最后两行代码】的位置呢，如果是jdk1.6呢
        System.out.println(x1 == x2); //false
    }
}
